package frontdesk

import (
	"individual/controllers"
	"individual/models"
	"math"
)

type BlogController struct {
	controllers.BaseController
}

// GetAllBlogs
// @Title	GetAllBlogs
// @Description 获取所有博客信息
// @Success 201 获取所有博客信息成功
// @Failure 403 获取所有博客信息失败
// @router /getAllBlogs/ [get]
func (c *BlogController) GetAllBlogs() {
	var blogs []models.Blog
	_, err := c.O.QueryTable(new(models.Blog).TableName()).Filter("blog_state", 1).OrderBy("-blog_top", "-id").All(&blogs)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取用户信息失败")
		c.ServeJSON()
		return
	}
	for i := 0; i < len(blogs); i++ {
		_, err = c.O.LoadRelated(&blogs[i], "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(&blogs[i], "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		blogs[i].View, _ = blogs[i].GetViews()
	}
	c.Data["json"] = models.GetRevaOk(blogs)
	c.ServeJSON()
}

// GetBlogById
// @Title	GetBlogById
// @Description 获取博客信息
// @Success 201 获取博客信息成功
// @Failure 403 获取博客信息失败
// @Param id path int true "博客id"
// @router /getBlogById/:id [get]
func (c *BlogController) GetBlogById() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "输入的格式错误")
		c.ServeJSON()
		return
	}
	var blog = models.Blog{Id: id}
	err = c.O.Read(&blog)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "查询博客失败")
		c.ServeJSON()
		return
	}
	_, err = c.O.LoadRelated(&blog, "Types")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
		c.ServeJSON()
		return
	}
	_, err = c.O.LoadRelated(&blog, "Labels")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
		c.ServeJSON()
		return
	}
	blog.View, _ = blog.GetViews()
	c.Data["json"] = models.GetRevaOk(blog)
	c.ServeJSON()
}

// GetTypes
// @Title GetTypes
// @Description 获取所有博客的分类
// @Success 201 获取所有博客的分类成功
// @Failure 403 获取所有博客的分类失败
// @router /getTypes/ [get]
func (c *BlogController) GetTypes() {
	var types []models.Type
	_, err := c.O.QueryTable(new(models.Type).TableName()).All(&types)
	type Help struct {
		Type models.Type
		Num  int64
	}
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取所有博客的分类失败")
		c.ServeJSON()
		return
	}
	var hs = make([]Help, len(types))
	for i, aType := range types {
		count, _ := c.O.QueryTable(new(models.Blog).TableName()+"_"+new(models.Type).TableName()+"s").Filter("tab_type_id", aType.Id).Count()
		hs[i].Type = aType
		hs[i].Num = count
	}
	c.Data["json"] = models.GetRevaOk(hs)
	c.ServeJSON()
}

// GetLabels
// @Title GetLabels
// @Description 获取所有博客的标签
// @Success 201 获取所有博客的标签成功
// @Failure 403 获取所有博客的标签失败
// @router /getLabels/ [get]
func (c *BlogController) GetLabels() {
	var labels []models.Label
	type Help struct {
		Label models.Label
		Num   int64
	}
	_, err := c.O.QueryTable(new(models.Label).TableName()).All(&labels)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取所有博客的标签失败")
		c.ServeJSON()
		return
	}
	var hs = make([]Help, len(labels))
	for i, label := range labels {
		count, _ := c.O.QueryTable(new(models.Blog).TableName()+"_"+new(models.Label).TableName()+"s").Filter("tab_label_id", label.Id).Count()
		hs[i].Label = label
		hs[i].Num = count
	}
	c.Data["json"] = models.GetRevaOk(hs)
	c.ServeJSON()
}

// GetBlogByLabelId
// @Title GetBlogByLabelId
// @Description 获取博客通过标签Id
// @Param	id 	path	int 	true	"标签id"
// @Success 201 根据标签id查博客信息成功
// @Failure 403 根据标签id查博客信息失败
// @router /getBlogByLabelId/:id [get]
func (c *BlogController) GetBlogByLabelId() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "id输入格式错误")
		c.ServeJSON()
		return
	}
	var label = models.Label{Id: id}
	err = c.O.Read(&label)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "没有该标签")
		c.ServeJSON()
		return
	}
	_, err = c.O.LoadRelated(&label, "Blogs")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
		c.ServeJSON()
		return
	}
	blogs := label.Blogs
	for i := 0; i < len(blogs); i++ {
		_, err = c.O.LoadRelated(blogs[i], "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(blogs[i], "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		blogs[i].View, _ = blogs[i].GetViews()
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"blogs":     blogs,
		"labelName": label.LabelName,
	})
	c.ServeJSON()
}

// GetBlogByTypeById
// @Title GetBlogByTypeById
// @Description 根据分类id查询博客信息
// @Param	id path	int 	true	"分类id"
// @Success 201 根据分类id查博客信息成功
// @Failure 403 根据分类id查博客信息失败
// @router /getBlogByTypeById/:id [get]
func (c *BlogController) GetBlogByTypeById() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "id输入格式错误")
		c.ServeJSON()
		return
	}
	var aType = models.Type{Id: id}
	err = c.O.Read(&aType)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "没有该分类")
		c.ServeJSON()
		return
	}
	_, err = c.O.LoadRelated(&aType, "Blogs")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
		c.ServeJSON()
		return
	}
	blogs := aType.Blogs
	for i := 0; i < len(blogs); i++ {
		_, err = c.O.LoadRelated(blogs[i], "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(blogs[i], "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		blogs[i].View, _ = blogs[i].GetViews()
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"blogs":    blogs,
		"typeName": aType.TypeName,
	})
	c.ServeJSON()
}

// GetBlogByPage
// @Title GetBlogByPage
// @Description 分页获取博客
// @Param pagesize	path	int true	"pagesize"
// @Param page	path	int true	"page"
// @Success 201 分页获取成功
// @Failure 403 分页获取失败
// @router /getBlogByPage/ [get]
func (c *BlogController) GetBlogByPage() {
	qs := c.O.QueryTable(new(models.Blog)).Filter("BlogState", 1).OrderBy("-blog_top", "-id")
	count, err := qs.Count()
	pageSetNum, err := c.GetInt("pagesize")
	if err != nil {
		pageSetNum = 1
	}
	//	总页数
	pageCount := math.Ceil(float64(count) / float64(pageSetNum))
	//	获取当前页码
	pageNum, err := c.GetInt("page")
	if err != nil {
		pageNum = 1
	}
	var blogs []models.Blog
	_, err = qs.Limit(pageSetNum, pageSetNum*(pageNum-1)).All(&blogs)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "错误")
		c.ServeJSON()
		return
	}
	for i := 0; i < len(blogs); i++ {
		_, err = c.O.LoadRelated(&blogs[i], "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(&blogs[i], "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		blogs[i].View, _ = blogs[i].GetViews()
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"num":       count,
		"pageNum":   pageNum,
		"pageCount": pageCount,
		"blogs":     blogs,
	})
	c.ServeJSON()
}

// GetBlogsByBlogName
// @Title GetBlogsByBlogName
// @Description 模糊查询博客
// @Param blogName	path	string true	"模糊查询"
// @Success 201 模糊查询博客成功
// @Failure 403 模糊查询博客失败
// @router /getBlogsByBlogName/ [get]
func (c *BlogController) GetBlogsByBlogName() {
	blogname := c.GetString("blogName")
	var blogs []models.Blog
	_, err := c.O.QueryTable(new(models.Blog).TableName()).Filter("BlogState", 1).Filter("blog_name__contains", blogname).OrderBy("-blog_top", "-id").All(&blogs)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "查询失败")
		c.ServeJSON()
		return
	}
	for i := 0; i < len(blogs); i++ {
		_, err = c.O.LoadRelated(&blogs[i], "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(&blogs[i], "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		blogs[i].View, _ = blogs[i].GetViews()
	}
	c.Data["json"] = models.GetRevaOk(blogs)
	c.ServeJSON()
}

// GetBlogViews
// @Title	GetBlogViews
// @Description Query a blogView
// @Param	id 	path	int true	"Query a blogView by ID"
// @Success 201 Query a blogView successfully
// @Failure 403 Failed to query a blogView
// @router /getBlogViews/:id [get]
func (c *BlogController) GetBlogViews() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取参数失败")
		c.ServeJSON()
		return
	}
	var blog = models.Blog{Id: id}
	views, err := blog.GetViews()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取bolg浏览量失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk(views)
	c.ServeJSON()
}
